package top.healthylife.gateway.platform.mysql.api.api;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.dynamic.datasource.toolkit.DynamicDataSourceContextHolder;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import top.healthylife.basics.utils.utils.SystemThreadLocal;
import top.healthylife.basics.utils.utils.TenantContextHolder;
import top.healthylife.gateway.core.service.ParentFace;
import top.healthylife.gateway.core.util.SystemConfigUtils;
import top.healthylife.gateway.dto.annotation.ApiServiceExplain;
import top.healthylife.gateway.platform.mysql.api.config.SystemConfig;
import top.healthylife.gateway.platform.mysql.api.entity.PersonalBlacklist;
import top.healthylife.gateway.platform.mysql.api.mapper.PersonalBlacklistMapper;
import top.healthylife.gateway.platform.mysql.dto.pblack.req.MySqlReqPersonalBlackListDto;
import top.healthylife.gateway.platform.mysql.dto.pblack.res.MySqlResPersonalBlackListDto;
import top.heathylife.gateway.common.cons.SystemCons;
import top.heathylife.gateway.common.po.RecordRequestInfo;

import java.util.List;


/**
 * @author GaoZexi
 * @datetime 2020/3/20 13:19
 */
@Slf4j
@Service
@ApiServiceExplain(price = 0, apiName = "个人黑名单", info = "根据条件查询个人黑名单的数据")
@AllArgsConstructor
public class PersonalBlackApi implements ParentFace<MySqlReqPersonalBlackListDto, MySqlResPersonalBlackListDto> {

    PersonalBlacklistMapper personalBlacklistMapper;

    @Override
    public MySqlResPersonalBlackListDto execute(MySqlReqPersonalBlackListDto t) {
        SystemConfig baseConfig = (SystemConfig) SystemConfigUtils.get();
        String name = baseConfig.getName();
        //切换数据源
        DynamicDataSourceContextHolder.push(name);
        log.info("切换数据源{}", name);
        TenantContextHolder.clear();
        LambdaQueryWrapper<PersonalBlacklist> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(ObjectUtil.isNotEmpty(t.getIdCard()), PersonalBlacklist::getIdCard, t.getIdCard())
                .eq(ObjectUtil.isNotEmpty(t.getPhoneNumber()), PersonalBlacklist::getPhoneNumber, t.getPhoneNumber())
                .eq(PersonalBlacklist::getRemoveStatus, "0")
                .eq(PersonalBlacklist::getArrearsStatus, "0");
        String body = JSONObject.toJSONString(t);
        log.info("个人黑名单,查询条件[{}]", body);
        RecordRequestInfo.recordRequest(name + "(数据源名称)", body, null, "数据库查询请求");
        List<PersonalBlacklist> personalBlacklists = personalBlacklistMapper.selectList(wrapper);
        if (ObjectUtil.isEmpty(personalBlacklists)) {
            log.info("个人黑名单,查询结果[未命中]");
            MySqlResPersonalBlackListDto mySqlResBusinessBlackListDto = new MySqlResPersonalBlackListDto(false, "未命中");
            SystemThreadLocal.set(SystemCons.ORIGIN_DATA, JSONObject.toJSONString(mySqlResBusinessBlackListDto));
            return mySqlResBusinessBlackListDto;
        }

        PersonalBlacklist personalBlacklist = personalBlacklists.get(0);
        //将三方元数据存储到线程变量
        SystemThreadLocal.set(SystemCons.ORIGIN_DATA, JSONObject.toJSONString(personalBlacklist));
        //返回数据
        MySqlResPersonalBlackListDto copy = BeanUtil.copyProperties(personalBlacklist, MySqlResPersonalBlackListDto.class);
        copy.setMatch(true).setMessage("命中");
        log.info("个人黑名单,查询结果[命中],数据=>{}", JSON.toJSONString(copy));
        return copy;
    }
}
